cmake_minimum_required(VERSION 2.8)

project(yoohoo-client)

# Qt support
find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})

# Set build type
IF(CMAKE_BUILD_TYPE MATCHES "^$")
	SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: Debug Release." FORCE)
ELSE(CMAKE_BUILD_TYPE MATCHES "^$")
	SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build, options are: Debug Release.")   
ENDIF(CMAKE_BUILD_TYPE MATCHES "^$")

if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
	set(YOOHOO_BUILD_TYPE debug)
else(${CMAKE_BUILD_TYPE} MATCHES "Debug")
	set(YOOHOO_BUILD_TYPE optimized)
	add_definitions(-DQT_NO_DEBUG_OUTPUT)
endif(${CMAKE_BUILD_TYPE} MATCHES "Debug")

# Set RPATH
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# Where to find header files
set(YOOHOO_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
include_directories(${YOOHOO_INCLUDE_DIR})

# Where to find QtCrypto header files
set(QT_CRYPTO_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/thirdparty/include/QtCrypto)
include_directories(${QT_CRYPTO_INCLUDE_DIR})

# Where to find thirdparty libraries
link_directories(${PROJECT_SOURCE_DIR}/thirdparty/lib)

# Where to find qca library
if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
message("$ENV{QCA_HOME}")
    link_directories(/home/roger/qca-2.0.1/lib)
endif("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")

# QtCrypto libraries
set (QT_CRYPTO_LIBRARY qca)

if(${CMAKE_BUILD_TYPE} MATCHES "Debug" AND WIN32)
	set(QT_CRYPTO_LIBRARY ${QT_CRYPTO_LIBRARY}d)
endif(${CMAKE_BUILD_TYPE} MATCHES "Debug" AND WIN32)

if(WIN32)
   set(QT_CRYPTO_LIBRARY ${YOOHOO_BUILD_TYPE} ${QT_CRYPTO_LIBRARY}2)
else(WIN32)
   set(QT_CRYPTO_LIBRARY ${YOOHOO_BUILD_TYPE} ${QT_CRYPTO_LIBRARY})
endif(WIN32)


# Set output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin CACHE
	PATH "Directory for all executables")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib CACHE
	PATH "Directory for all dynamic libraries")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib CACHE
	PATH "Directory for all static libraries")

# remember bin path to copy all dlls to test bin directory
if(WIN32)
	set(YOOHOO_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
endif(WIN32)
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
	set(YOOHOO_BINARY_DIR ${YOOHOO_BINARY_DIR}/Debug)
else(${CMAKE_BUILD_TYPE} MATCHES "Debug")
	set(YOOHOO_BINARY_DIR ${YOOHOO_BINARY_DIR}/Release)
endif(${CMAKE_BUILD_TYPE} MATCHES "Debug")

file(GLOB source_dir_files RELATIVE ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/src/*)
foreach(source_dir_file ${source_dir_files})
	if(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/src/${source_dir_file} AND NOT ${PROJECT_SOURCE_DIR}/src/${source_dir_file} MATCHES .svn)
		set(YOOHOO_MODULES ${YOOHOO_MODULES} ${source_dir_file})
	endif(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/src/${source_dir_file} AND NOT ${PROJECT_SOURCE_DIR}/src/${source_dir_file} MATCHES .svn)
endforeach(source_dir_file ${source_dir_files})

file(GLOB source_dir_files RELATIVE ${PROJECT_SOURCE_DIR}/extensions/xep/src ${PROJECT_SOURCE_DIR}/extensions/xep/src/*)
foreach(source_dir_file ${source_dir_files})
	if(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/extensions/xep/src/${source_dir_file} AND NOT ${PROJECT_SOURCE_DIR}/extensions/xep/src/${source_dir_file} MATCHES .svn)
		set(YOOHOO_XEP_MODULES ${YOOHOO_XEP_MODULES} ${source_dir_file})
	endif(IS_DIRECTORY ${PROJECT_SOURCE_DIR}/extensions/xep/src/${source_dir_file} AND NOT ${PROJECT_SOURCE_DIR}/extensions/xep/src/${source_dir_file} MATCHES .svn)
endforeach(source_dir_file ${source_dir_files})

# i18n
set(YOOHOO_FILES_TO_TRANSLATE)

foreach(module ${YOOHOO_MODULES})
	file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/${module}/*.cpp)
	set(YOOHOO_FILES_TO_TRANSLATE ${YOOHOO_FILES_TO_TRANSLATE} ${SRC_FILES})
	file(GLOB HDR_FILES ${YOOHOO_INCLUDE_DIR}/${module}/*.h)
	set(YOOHOO_FILES_TO_TRANSLATE ${YOOHOO_FILES_TO_TRANSLATE} ${HDR_FILES})
endforeach(module ${YOOHOO_MODULES})

option(UPDATE_TRANSLATIONS "Update source translation i18n/*.ts
	files (WARNING: make clean will delete the source .ts files! Danger!)")

set(YOOHOO_TRANSLATION_FILES i18n/yoohoo_zh_CN.ts)

if(UPDATE_TRANSLATIONS)
  qt4_create_translation(YOOHOO_QM_FILES ${YOOHOO_FILES_TO_TRANSLATE} ${YOOHOO_TRANSLATION_FILES})
else(UPDATE_TRANSLATIONS)
  qt4_add_translation(YOOHOO_QM_FILES ${YOOHOO_TRANSLATION_FILES})
endif(UPDATE_TRANSLATIONS)

set(YOOHOO_TRANSLATIONS_OUTPUT i18n/yoohoo_zh_CN.qm)
add_custom_command(OUTPUT ${YOOHOO_TRANSLATIONS_OUTPUT}
	COMMAND ${CMAKE_COMMAND} ARGS -E copy ${PROJECT_SOURCE_DIR}/i18n/qt_zh_CN.qm ${PROJECT_BINARY_DIR}/i18n/qt_zh_CN.qm
	COMMAND ${CMAKE_COMMAND} ARGS -E copy ${PROJECT_BINARY_DIR}/ql_zh_CN.qm ${PROJECT_BINARY_DIR}/i18n/yoohoo_zh_CN.qm
	COMMAND ${CMAKE_COMMAND} ARGS -E remove ${PROJECT_BINARY_DIR}/ql_zh_CN.qm
	DEPENDS ${YOOHOO_QM_FILES}
)

add_custom_target(translations DEPENDS ${YOOHOO_TRANSLATIONS_OUTPUT} VERBATIM)

include(${PROJECT_SOURCE_DIR}/utils.cmake)

# Build src directory
add_subdirectory(src)

# testing support
enable_testing()
# Build test directory
add_subdirectory(test)

# Build extensions directory
add_subdirectory(extensions)

execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/images ${PROJECT_BINARY_DIR}/images)
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_BINARY_DIR}/images/.svn)
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_BINARY_DIR}/images/debugwindow/.svn)
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_BINARY_DIR}/images/system/.svn)
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_BINARY_DIR}/images/status/.svn)
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_BINARY_DIR}/images/emotionsets/.svn)
execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_BINARY_DIR}/images/emotionsets/default/.svn)

if(WIN32)
	if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/qt/QtCored4.dll ${PROJECT_BINARY_DIR}/bin/Debug/QtCored4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/qt/QtGuid4.dll ${PROJECT_BINARY_DIR}/bin/Debug/QtGuid4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/qt/QtNetworkd4.dll ${PROJECT_BINARY_DIR}/bin/Debug/QtNetworkd4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/qcad2.dll ${PROJECT_BINARY_DIR}/bin/Debug/qcad2.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/ssleay32.dll ${PROJECT_BINARY_DIR}/bin/Debug/ssleay32.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/libeay32.dll ${PROJECT_BINARY_DIR}/bin/Debug/libeay32.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/libsasl.dll ${PROJECT_BINARY_DIR}/bin/Debug/libsasl.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/saslDIGESTMD5.dll ${PROJECT_BINARY_DIR}/bin/Debug/saslDIGESTMD5.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/crypto/qca-ossld2.dll ${PROJECT_BINARY_DIR}/plugins/crypto/qca-ossld2.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/crypto/qca-cyrus-sasld2.dll ${PROJECT_BINARY_DIR}/plugins/crypto/qca-cyrus-sasld2.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/imageformats/qgifd4.dll ${PROJECT_BINARY_DIR}/plugins/imageformats/qgifd4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/imageformats/qjpegd4.dll ${PROJECT_BINARY_DIR}/plugins/imageformats/qjpegd4.dll)
	else(${CMAKE_BUILD_TYPE} MATCHES "Release")
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/qt/QtCore4.dll ${PROJECT_BINARY_DIR}/bin/Release/QtCore4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/qt/QtGui4.dll ${PROJECT_BINARY_DIR}/bin/Release/QtGui4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/qt/QtNetwork4.dll ${PROJECT_BINARY_DIR}/bin/Release/QtNetwork4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/qca2.dll ${PROJECT_BINARY_DIR}/bin/Release/qca2.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/ssleay32.dll ${PROJECT_BINARY_DIR}/bin/Release/ssleay32.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/libeay32.dll ${PROJECT_BINARY_DIR}/bin/Release/libeay32.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/libsasl.dll ${PROJECT_BINARY_DIR}/bin/Release/libsasl.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/saslDIGESTMD5.dll ${PROJECT_BINARY_DIR}/bin/Release/saslDIGESTMD5.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/crypto/qca-ossl2.dll ${PROJECT_BINARY_DIR}/plugins/crypto/qca-ossl2.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/crypto/qca-cyrus-sasl2.dll ${PROJECT_BINARY_DIR}/plugins/crypto/qca-cyrus-sasl2.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/imageformats/qgif4.dll ${PROJECT_BINARY_DIR}/plugins/imageformats/qgif4.dll)
		execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/thirdparty/lib/plugins/imageformats/qjpeg4.dll ${PROJECT_BINARY_DIR}/plugins/imageformats/qjpeg4.dll)
	endif(${CMAKE_BUILD_TYPE} MATCHES "Debug")
endif(WIN32)
